//
//  CAIBehaviorAttack.cpp
//  beast_tamer
//
//  Created by  bk on 13/1/22.
//
//

#include "CAIBehaviorAttack.h"
#include "CPlayerControl.h"
#include "AnimationDef.h"
#include "CMemberCallback.h"


CAIBehaviorAttack::CAIBehaviorAttack()
{
    m_isStartTime = true;
    
    m_StartTime = 0;
}

CAIBehaviorAttack::~CAIBehaviorAttack()
{
    
}

bool CAIBehaviorAttack::Init(CAIManager* ptr)
{
    if (ptr)
    {
        m_AiManager = ptr;
        
        Register(Move,&CAIBehaviorAttack::onProcessMove);
        Register(Attack,&CAIBehaviorAttack::onProcessAttack);
        
        //onHandleDamage
        CCallBack* FunCallBack = new CMemberCallback<CAIBehaviorAttack>(this,&CAIBehaviorAttack::onHandleDamage);
        MyMaster()->RegisterAnimationCallBack(ACTIVE_ANIMATION_ID_ATTACK, FunCallBack);
        return true;
    }
    
    return false;
}

void CAIBehaviorAttack::Entering(IStateInput* input)
{
    targetObject  = ((CAttackCommand*)input)->targetObj;
    
    if (MyMaster()->GetPoint().CheckDistance(targetObject->GetPoint(), MonsterDistance))
    {
        m_nCurrProcess = Attack;
        
        //MyMaster()->ChangeActiveAnimation(ACTIVE_ANIMATION_ID_ATTACK);
        
        m_isStartTime = true;
    }
    else
    {
        m_nCurrProcess = Move;
        
        MyMaster()->ChangeActiveAnimation(ACTIVE_ANIMATION_ID_MOVE);
    }
}


void CAIBehaviorAttack::Update(IStateInput* pInput)
{
    if (m_isStartTime)
    {
        
        m_StartTime += static_cast<CAiTimeInput*>(pInput)->dt;
    }
    
    this->processFun(m_nCurrProcess);
}

void CAIBehaviorAttack::Leaving(IStateInput*)
{
    
}

int CAIBehaviorAttack::ShouldChange(IStateInput*)
{
    if (m_ShouldExit)
    {
        return BATTLE_OBJECT_STATE_WAIT_COMMAND;
    }
    
    return INVALID;
}

void CAIBehaviorAttack::onProcessMove()
{
    if (MyMaster()->GetCurrActiveAnimation() != ACTIVE_ANIMATION_ID_MOVE)
    {
        MyMaster()->ChangeActiveAnimation(ACTIVE_ANIMATION_ID_MOVE);
        
        return;
    }
    
    if(MyMaster()->GetPoint().Step(targetObject->GetPoint(), targetObject->GetPoint(), MonsterDistance))
    {
        m_nCurrProcess = Attack;
        
        //MyMaster()->ChangeActiveAnimation(ACTIVE_ANIMATION_ID_ATTACK);
        
        m_isStartTime = true;
    }
}

void CAIBehaviorAttack::onProcessAttack()
{
    
    if (!m_isStartTime)
    {
        return;
    }
    else
    {
        if (m_StartTime < START_ATTACK_TIME)
        {
            if (MyMaster()->GetPoint().CheckDistance(targetObject->GetPoint(), ATTACK_DISTANCE_X+5) == false)
            {
                m_nCurrProcess = Move;
                
                MyMaster()->ChangeActiveAnimation(ACTIVE_ANIMATION_ID_MOVE);
            }
            
            return;
        }
        else
        {
            MyMaster()->ChangeActiveAnimation(ACTIVE_ANIMATION_ID_ATTACK);
            
            m_isStartTime = false;
            
            m_StartTime = 0;
            
            return;
        }
    }
    
    /*
    if (MyMaster()->GetCurrActiveAnimation() != ACTIVE_ANIMATION_ID_ATTACK)
    {
        MyMaster()->ChangeActiveAnimation(ACTIVE_ANIMATION_ID_ATTACK);
        
        return;
    }
    
    if (MyMaster()->GetPoint().CheckDistance(targetObject->GetPoint(), MonsterDistance) == false)
    {
        m_nCurrProcess = Move;
        
        MyMaster()->ChangeActiveAnimation(ACTIVE_ANIMATION_ID_MOVE);
    }
    */
}

void CAIBehaviorAttack::onHandleDamage(void* Data)
{
    if (targetObject->isDie())
    {
        m_ShouldExit = true;
        
        return;
    }
    
    
    m_isStartTime = true;
}



